<template>
	<view class="work-container">
		<button @click="scanStartFuncS()">{{procode}}</button>
		<br />
		<button @click="scanStartFuncP()">{{usercode}}</button>
		<br />
		<button type="primary" @click="scanStartFunc()">上工</button>
	</view>
</template>

<script>
  import { staffCheckIn } from "@/api/pro_spinning/staff_check_in"
	//获取module
	var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule")
	const modal = uni.requireNativePlugin('modal');

	var globalEvent = uni.requireNativePlugin('globalEvent');
	export default {
		onLoad() {
			var _this = this;
			globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
				if (_this.funType == "S") {
					_this.procode = e.barcode;
				}
				if (_this.funType == "P") {
					_this.usercode = e.barcode;
				}
				//连续扫描如果间隔时间比较短，会出现toast不提示的情况，数据可以正常接收到，建议查看控制台输出
				console.log(JSON.stringify(e));
				_this.funType = "";
			});
			barcodeModel.initScan((ret) => {});
		},
		onUnload() {
			barcodeModel.closeScan((ret) => {
				// modal.toast({
				// 	message: ret,
				// 	duration: 1.5
				// });
			});
			globalEvent.removeEventListener('iDataBarcodeEvent');
		},
		data() {
			return {
				funType: "",
				procode: "扫描机台号",
				usercode: "扫描员工码",
				staffCheckInVO: {
					machine: undefined,
					username: undefined
				}
			}
		},
		methods: {
			//上工
			scanStartFunc() {
				var _this = this;
				staffCheckIn(_this.procode, _this.usercode).then(response => {
					console.log(response);
				  this.$modal.msgSuccess("修改成功")
				})
			},
			//扫丝码
			scanStartFuncS() {
				var _this = this;
				barcodeModel.scanStart((ret) => {
					_this.funType = "S";
				});
			},
			//扫员工码
			scanStartFuncP() {
				var _this = this;
				barcodeModel.scanStart((ret) => {
					_this.funType = "P";
				});
			}
		}
	}
</script>

<style lang="scss">
	.work-container {
		padding: 20px;
		;
	}

	.title-text {
		font-size: 28rpx;
		white-space: normal;
		/* 允许换行 */
		word-wrap: break-word;
		/* 允许长单词或 URL 换行 */
		/* 如果父容器有固定宽度，确保宽度生效 */
	}

	.multiline-text {
		font-size: 24rpx;
		color: #777;
		white-space: normal;
		/* 允许换行 */
		word-wrap: break-word;
		/* 允许长单词或 URL 换行 */
		/* 如果父容器有固定宽度，确保宽度生效 */
	}
</style>